package com.zlyx.easy.database;

import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.beans.factory.annotation.Autowired;

import com.zlyx.easy.core.annotations.SpringBean;
import com.zlyx.easy.core.utils.ObjectUtils;
import com.zlyx.easy.database.marker.DaoMarker;

/**
 * @Auth 赵光
 * @Describle
 * @2019年1月14日 上午11:49:45
 */
@SpringBean(todo = { "dao层工厂" })
public class DaoMarkerFactory {
	
	private static ConcurrentMap<Field, String> cache = new ConcurrentHashMap<>();
	
    private static DaoMarker daoMarker;
    
    public DaoMarkerFactory(@Autowired(required = false) DaoMarker daoMarker) {
		if (daoMarker == null) {
			daoMarker = new DaoMarker() {
			};
		}
    	DaoMarkerFactory.daoMarker = daoMarker;
    }

	public static String getFieldName(Field field) throws Exception {
		String fieldName = cache.get(field);
		if (ObjectUtils.isEmpty(fieldName)) {
			fieldName = daoMarker.parse(field);
			cache.put(field, fieldName);
		}
		return fieldName;
	}
}
